import {
	ref
} from 'vue';

export const data = ref({
	"playUrl": "",
	"songmid": "000TSmhd4TunoV",
	"name": "天ノ弱",
	"imageUrl": "https://y.gtimg.cn/music/photo_new/T002R300x300M000002EV44m1zkFax.jpg?max_age=2592000"
})




// 播放暂停 函数  
const AudioPlay = (innerAudioContext) => {
	if (innerAudioContext.paused) {
		innerAudioContext.play();
	} else {
		innerAudioContext.pause();
	}
};

// 音乐播放
export const player = ref({
	isPlay: false,
	currentTime: 0,
	duration: 0,
	AudioPlay: AudioPlay
});

export const innerAudioContext = uni.createInnerAudioContext()



// 格式化音乐播放器
export const initializeInnerAudioContext = (innerAudioContext) => {
	innerAudioContext.onCanplay(() => {
		player.value.duration = innerAudioContext.duration;
		if (!innerAudioContext.paused) {
			innerAudioContext.play();
		}
	});
	innerAudioContext.onTimeUpdate(() => {
		player.value.currentTime = innerAudioContext.currentTime;
	});
	innerAudioContext.onPause(() => {
		player.value.isPlay = false;
	});

	innerAudioContext.onPlay(() => {
		player.value.isPlay = true;
	});
	innerAudioContext.onEnded(() => {
		innerAudioContext.stop();
		player.value.isPlay = false;
	});
}

initializeInnerAudioContext(innerAudioContext)

// 格式化时间函数  
export const formatTime = (seconds) => {
	const minutes = Math.floor(seconds / 60);
	const remainingSeconds = Math.floor(seconds % 60);
	const formattedMinutes = String(minutes).padStart(2, '0');
	const formattedSeconds = String(remainingSeconds).padStart(2, '0');
	return `${formattedMinutes}:${formattedSeconds}`;
};